<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cashhistory_model extends CI_Model {

	function __construct() {
		parent::__construct();
		
	}

	/**
	 * 회원의 포인트 적립/차감 내역
	 */
	function select_cash_history_list($user_id, $page_no, $page_size) {
		log_message('debug',"#### Cashhistory_model::select_cash_history_list($user_id)");

		$start_row = ($page_no-1) * $page_size;

		$sql  = PHP_EOL." SELECT A.* ";
		$sql .= PHP_EOL." FROM ";
		$sql .= PHP_EOL." ( ";
		$sql .= PHP_EOL."     SELECT  ";
		$sql .= PHP_EOL."           P.user_id ";
		$sql .= PHP_EOL."         , IF(P.point_type = 'C', '캐쉬충전', '보너스포인트') point_type";
		$sql .= PHP_EOL."         , IF(P.cash=0,0,CONCAT('+',P.cash)) cash ";
		$sql .= PHP_EOL."         , IF(P.point=0,0,CONCAT('+',P.point)) point ";
		$sql .= PHP_EOL."         , (SELECT actv_caption FROM tb_activity_to_point WHERE actv_id = P.actv_id) subject ";
		$sql .= PHP_EOL."         , create_dt ";
		$sql .= PHP_EOL."     FROM  tb_point_charged_history P";
		$sql .= PHP_EOL."  ";
		$sql .= PHP_EOL."     UNION ";
		$sql .= PHP_EOL."  ";
		$sql .= PHP_EOL."     SELECT ";
		$sql .= PHP_EOL."           S.user_id ";
		$sql .= PHP_EOL."         , '' point_type ";
		$sql .= PHP_EOL."         , IF(S.cash=0,0,CONCAT('-',S.cash)) cash ";
		$sql .= PHP_EOL."         , IF(S.point=0,0,CONCAT('-',S.point)) point ";
		$sql .= PHP_EOL."         , (SELECT service_name FROM tb_service WHERE service_id = S.service_id) subject ";
		$sql .= PHP_EOL."         , S.create_dt ";
		$sql .= PHP_EOL."     FROM  tb_service_used_history S";
		$sql .= PHP_EOL." ) A ";
		$sql .= PHP_EOL." WHERE A.user_id = ? ";
		$sql .= PHP_EOL." AND   (A.cash <> 0) ";
		$sql .= PHP_EOL." ORDER BY A.create_dt DESC ";
		$sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 총 글 수
	 */
	function get_cash_history_total_count($user_id) {
		log_message('debug',"#### Cashhistory_model::get_cash_history_total_count($user_id)");

		$this->db->where('user_id', $user_id);
		$this->db->where('cash <>', 0);
		$this->db->from('tb_point_charged_history');
		$result1 = $this->db->count_all_results() or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
		
		$this->db->where('user_id', $user_id);
		$this->db->where('cash <>', 0);
		$this->db->from('tb_service_used_history');
		$result2 = $this->db->count_all_results() or $this->logsql->log_sql('error');

		$this->logsql->log_sql();

		return $result1+$result2;
	}
	
	/**
	 * 소멸 예정 캐쉬 리스트
	 */
	function select_cash_history_hurry_list($user_id) {
		log_message('debug',"#### Cashhistory_model::select_cash_history_hurry_list($user_id)");
		
		$this->db->where("user_id", $user_id);
		$this->db->where("cash_balance > ",'0', FALSE);
		$this->db->where("DATE_FORMAT(CURDATE(),'%Y%m%d') BETWEEN ","start_dt AND end_dt", FALSE);
		$this->db->order_by('idx','asc');
		$query = $this->db->get('tb_point_charged_history');
		$result = $query->result_array();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $result;
	}
}

/* End of file cashhistory_model.php */
/* Location: ./application/models/mypage/cashhistory_model.php */